package gcb.metalsys.service.cop.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import gcb.metalsys.entity.cop.CopProduct;
import gcb.metalsys.entity.cop.ProPrice;
import gcb.metalsys.enums.UploadEnum;
import gcb.metalsys.exception.UploadException;
import gcb.metalsys.mapper.cop.ProPriceMapper;
import gcb.metalsys.service.cop.ProPriceService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2020-07-15
 */
@Service
public class ProPriceServiceImpl extends ServiceImpl<ProPriceMapper, ProPrice> implements ProPriceService {

    @Override
    public void readExcel(MultipartFile multipartFile) throws RuntimeException {
        String filePath = multipartFile.getOriginalFilename();

        String extString = filePath.substring(filePath.lastIndexOf("."));
        InputStream inputStream = null;
        Workbook workbook = null;
        List<ProPrice> priceList = new ArrayList<>();

        try {
            inputStream = multipartFile.getInputStream();
            if (".xls".equals(extString)) {
                workbook = new HSSFWorkbook(inputStream);
            } else if (".xlsx".equals(extString)) {
                workbook = new XSSFWorkbook(inputStream);
            }

            Sheet sheet = workbook.getSheetAt(0);
            int rows = sheet.getLastRowNum() + 1;
            Row tmp = sheet.getRow(0);
            int cols = tmp.getPhysicalNumberOfCells();

            DecimalFormat df = new DecimalFormat("0");
            for (int row = 1; row < rows; row++) {
                Row r = sheet.getRow(row);
                ProPrice proPrice = new ProPrice(r.getCell(0).getStringCellValue(),
                        timeChange(df.format(r.getCell(1).getNumericCellValue()),1),
                        timeChange(df.format(r.getCell(2).getNumericCellValue()),0),
                        timeChange(df.format(r.getCell(3).getNumericCellValue()),0),
                        new Integer(r.getCell(4).getStringCellValue()),
                        new BigDecimal(r.getCell(5).getNumericCellValue()));

                priceList.add(proPrice);
            }

            saveBatch(priceList);
        } catch (Exception e) {
            throw new UploadException(UploadEnum.ERROR);
        }
    }

    private String timeChange(String time,int type){
        StringBuilder result = new StringBuilder(time);
        if (type == 0) {
            if (result.length()==5) {
                result.insert(1,":");
                result.insert(4,":");
            }else if(result.length()==4){
                result.insert(0,"00:");
                result.insert(5,":");
            }else if(result.length()==1){
                return "00:00:00";
            } else{
                result.insert(2,":");
                result.insert(5,":");
            }
        }else{
            result.insert(4,".");
            result.insert(7,".");
        }

        return result.toString();
    }

}
